'use strict';
/**
 * Created by FreezeSoul on 2016/1/18.
 */
define([
        'angular',
        'app/core/editor'
    ],
    function(angular) {
        angular.module('ad.editor').controller(
            "adExcelPluginEditorController",['$rootScope','$scope','_','Notification',function($rootScope,$scope,_,Notification) {

                if(!$scope.extendOfIndex.columnBindings){
                    $scope.extendOfIndex.columnBindings = [];
                    /*
                    * {
                    *   ColumnName,
                    *   ColumnCode
                    * }
                    * */
                }

                $scope.compareBindings = function () {
                    if($scope.bindingOfIndex.length == 0) return;
                    var index = $scope.extendOfIndex.columnBindings.length-1;
                    while(index>=0){
                        var binding = $scope.extendOfIndex.columnBindings[index];
                        if(!(
                                _.findWhere($scope.bindingOfIndex[0].Dimensions,{
                                    Code:binding.ColumnCode,
                                    Column:binding.Column,
                                    Name:binding.ColumnName
                                })
                            ) &&
                            !(
                                _.findWhere($scope.bindingOfIndex[0].Measures,{
                                    Code:binding.ColumnCode,
                                    Column:binding.Column,
                                    Name:binding.ColumnName
                                })
                            )){
                            $scope.extendOfIndex.columnBindings.splice(index, 1);
                        }
                        index--;
                    }
                    _.each($scope.bindingOfIndex[0].Dimensions, function (dimension) {
                        if(! _.findWhere($scope.extendOfIndex.columnBindings,{
                                ColumnCode:dimension.Code
                            })){
                            $scope.extendOfIndex.columnBindings.push({
                                ColumnName:dimension.Name,
                                Column:dimension.Column,
                                ColumnCode:dimension.Code
                            })
                        }
                    });

                    _.each($scope.bindingOfIndex[0].Measures, function (measure) {
                        if(! _.findWhere($scope.extendOfIndex.columnBindings,{
                                ColumnCode:measure.Code
                            })){
                            $scope.extendOfIndex.columnBindings.push({
                                ColumnName:measure.Name,
                                Column:measure.Column,
                                ColumnCode:measure.Code
                            })
                        }
                    });
                };
                
                $scope.dragControlListeners = {
                    containment: '.bindingSortContainer'
                };

                $scope.$on("ad.index.binding.changed", function (event, args) {
                    $scope.compareBindings();
                });

                $scope.eventSelect = function () {
                    $scope.bindPivotEvent({
                        Event:"ExcelClick",
                        Params:[
                            {Value:"Cell",Name:"单元格点击"},
                        ]
                    });
                };
            }]
        );
    });